package com.devunion.bogdan.view.toolbar;

import org.apache.commons.io.FilenameUtils;

import javax.swing.*;

import com.devunion.bogdan.util.IconHelper;

/**
 * @author Viktar Liaskovich
 */
public class ToolbarButton extends JButton {
    private static final int DEFAULT_TOOLBAR_BUTTON_SIZE = 24;

    public void setIcon(Icon defaultIcon) {

        if (defaultIcon instanceof ImageIcon) {
            String description = ((ImageIcon) defaultIcon).getDescription();

            if (description.endsWith("48.gif")) {
                String iconBaseName = FilenameUtils.getBaseName(description);
                String iconExtension = FilenameUtils.getExtension(description);

                String newIconName = iconBaseName.substring(0, iconBaseName.length()-2) + DEFAULT_TOOLBAR_BUTTON_SIZE + "." + iconExtension;

                super.setIcon(IconHelper.getIcon(newIconName));
            } else {
                super.setIcon(defaultIcon);
            }
        } else {
            super.setIcon(defaultIcon);
        }
    }
}
